﻿using System;
using System.Collections.Generic;
using System.Text;
using LibNoise;
using System.Drawing;
using SampleImageGenerator.Modules.Primitives;

namespace SampleImageGenerator.Rendering
{
	internal class ModulePalette : IPalette
	{
		public String Name { get; set; }
		public IModule Module { get; set; }

		public ModulePalette()
			: this(new Constant())
		{ 
		}

		public ModulePalette(IModule module)
		{
			if (module == null)
			{
				throw new ArgumentNullException("module");
			}

			Module = module; 
		}

		public Color GetColour(Double value)
		{
			Double moduleRed = Module.GetValue(value, 0, 0);
			Double scaledModuleRed = (moduleRed + 1) / 2;
			Int32 integerRed = (Int32)(scaledModuleRed * 255);
			Int32 clampedRed = LibNoise.Math.ClampValue(integerRed, 0, 255);

			Double moduleGreen = Module.GetValue(0, value, 0);
			Double scaledModuleGreen = (moduleGreen + 1) / 2;
			Int32 integerGreen = (Int32)(scaledModuleGreen * 255);
			Int32 clampedGreen = LibNoise.Math.ClampValue(integerGreen, 0, 255);

			Double moduleBlue = Module.GetValue(0, 0, value);
			Double scaledModuleBlue = (moduleBlue + 1) / 2;
			Int32 integerBlue = (Int32)(scaledModuleBlue * 255);
			Int32 clampedBlue = LibNoise.Math.ClampValue(integerBlue, 0, 255);

			Color color = Color.FromArgb(255, clampedRed, clampedGreen, clampedBlue);
			return color; 
		}
		
		public void SetRange(Double minValue, Double maxValue)
		{
			// No need to set a range for this palette; there's 
			// no maximum or minimum that we can't handle. 
		}
	}
}
